// pages/wxlogin/index.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '', // token
    isgetIinformation: false,
    datates: '',
    show: false,
    StatusBar: 20,
    TheAgreementHasBeenAgreed: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      StatusBar: app.globalData.StatusBar
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getSchoolList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getUserInfo: function () {
    var that = this ;
    that.setData({
      isgetIinformation: true
    })
    wx.showLoading({
      title: '正在获取中...',
    })
    wx.login({
      success (res) {
        if (res.code) {
          console.log(res);
          let code = res.code
          util.request('POST', api.getopenid, {
            code: code
          }).then(function (res) {
            console.log(res);
            wx.setStorageSync('TwoPlusTravel-openid', {openid: res.openid, session_key: res.session_key})
            wx.hideLoading()
            that.setData({
              show: false
            })
          }).catch((res) => {
            wx.hideLoading()
            that.setData({
              show: true
            })
          })
        } else {
          that.setData({
            isgetIinformation: false
          })
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  // 登录授权
  getphonenumber: function (e) {
    if (!this.data.TheAgreementHasBeenAgreed) {
      wx.showToast({
        title: '请阅读并同意',
        icon: 'none'
      })
      return
    }
    var that = this ;
    that.setData({
      isgetIinformation: true
    })
    wx.showLoading({
      title: '正在获取中...',
    })
    var detail = e.detail
    if (detail.errMsg == "getPhoneNumber:ok") {
      var code = detail.code
      util.request('POST', api.Login, {
        code: code
      }).then(function (res) {
        if (res.code == 200) {
          let token = res.data
          let plateNumber = res.plateNumber
          wx.login({
            success (res) {
              if (res.code) {
                console.log(res);
                util.request('POST', api.getopenid, {
                  code: res.code
                }).then(function (res) {
                  console.log(res);
                  if (res.openid) {
                    wx.setStorageSync('TwoPlusTravel', {openid: res.openid, session_key: res.session_key, phone: token})
                    if (plateNumber) {
                      wx.setStorageSync('TwoPlusTravel-plateNumber', plateNumber)
                    }
                    wx.hideLoading()
                    wx.showToast({
                      title: '登录成功！'
                    })
                    let time = setTimeout(() => {
                      wx.navigateBack()
                      clearTimeout(time)
                    }, 500)
                  }
                }).catch((res) => {
                  wx.hideLoading()
                  wx.showToast({
                    title: '登录失败！',
                    icon: 'none'
                  })
                  that.setData({
                    isgetIinformation: false
                  })
                })
                let code = res.code
              } else {
                wx.hideLoading()
                wx.showToast({
                  title: '登录失败 请稍后再试',
                  icon: 'none'
                })
                that.setData({
                  isgetIinformation: false
                })
                console.log('登录失败！' + res.errMsg)
              }
            }
          })
        }else {
          wx.hideLoading()
          wx.showToast({
            title: '登录失败 请稍后再试',
            icon: 'none'
          })
          that.setData({
            isgetIinformation: false
          })
        }
      }).catch((res) => {
        console.log(res);
        wx.hideLoading()
        wx.showToast({
          title: '授权失败请稍后再试',
          icon: 'none'
        })
        that.setData({
          isgetIinformation: false
        })
      })
    }else {
      wx.hideLoading()
      wx.showToast({
        title: '已取消授权 请重新点击登录',
        icon: "none"
      })
      that.setData({
        isgetIinformation: false
      })
    }
  },
  cancel () {
    this.setData({
      show: false
    })
  },
  MyScheduleOrder () {
    // 获取页面栈
    const pages = getCurrentPages();
    // 上一层页面是页面栈的倒数第二个
    const prevPage = pages[pages.length - 2];
    // 如果上一层页面存在，则可以返回
    if (prevPage) {
      wx.navigateBack();
    } else {
      // 没有上一层页面的处理逻辑
      wx.reLaunch({
        url: '/pages/index/index'
      })
    }
  },
  handleOpenPrivacyContract() {
    // 打开隐私协议页面
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  },
  platformRules () {
    wx.navigateTo({
      url: '/pages/components/platformRules/index',
    })
  },
  protocolValidation () {
    let data = this.data.TheAgreementHasBeenAgreed
    if (data) {
      wx.removeStorageSync('empower')
    }else {
      wx.setStorageSync('empower', true)
    }
    this.setData({
      TheAgreementHasBeenAgreed: !this.data.TheAgreementHasBeenAgreed
    })
  }
})